import $HTTP from '@/shop_project/common/lib/request.js';
import tools from '@/shop_project/common/js/tools.js';
import user from "@/store/modules/user.js"
export default {
	state: {
		// 购物车存储的商品列表
		lists: [],
		// 选中列表（存放选中的goods_id）
		selectedList: [],
		// 选择列表 (存放选中的rec_id)
		selectedRecId: [],
		// 为避免多次触发导致数据未生成
		lodingStatus: false,
		// 选中列表（存放选中的obj）
		selectedListObj: [],
		// 由立即购买到 提交订单的数据
		shopOrderArrInfo:[]
	},
	getters: {
		// 合计金额 不包含礼包
		totalPrice: (state) => {
			var total = 0;
			state.lists.forEach(v => {
				v.list.forEach(k => {
					if ((state.selectedList.indexOf(k.goods_id) > -1) && (state.selectedRecId.indexOf(k.rec_id) > -1)) {
						// 不可简写
						total = tools.accAdd(total, tools.accMul(+k.goods_price, +k.goods_number))
					}
				})

			})
			state.discountPrice = total;
			state.lodingStatus = false;

			return +total;
		},
		// 判断是否全选
		checkedAll: (state) => {
			var count = state.lists.map(v => {
				return v.list.length;
			})
			return tools.accumulation(count) === state.selectedList.length
		},
		// 根据selectedRecId 里面的id 获取lists 指定商品
		filterListRecIdObj(state) {
			var arr = [];
			state.lists.forEach(v => {
				v.list.forEach(k => {
					if (state.selectedRecId.indexOf(k.rec_id) > -1) {
						arr.push(k);
					}
				})
			})
			// state.selectedListObj = arr;
			return arr
		},
	},
	mutations: {
		// 初始化list
		initCartList(state, lists) {
			state.lists = lists
		},
		// 选中/取消选中某个商品
		selectItem(state, index) {
			state.lodingStatus = true;
			let arr = index.split(',')
			let id = state.lists[arr[0]].list[arr[1]].goods_id,
				recId = state.lists[arr[0]].list[arr[1]].rec_id;
			let i = state.selectedRecId.indexOf(recId)
			// 之前是选中，取消选中 
			if (i > -1) {
				// 取消当前商品选中状态
				state.lists[arr[0]].list[arr[1]].checked = false;
				// 移除productid 
				state.selectedRecId.splice(state.selectedRecId.indexOf(recId), 1);
				// 移除选中列表中的当前商品
				return state.selectedList.splice(i, 1)
			}
			// 选中
			state.lists[arr[0]].list[arr[1]].checked = true
			state.selectedList.push(id)
			state.selectedRecId.push(recId)
		},
		// 全选
		selectAll(state) {
			var selectedListArr = state.lists.map(v => {
				return v.list.map(k => {
					// 设置选中状态
					k.checked = true;
					// 为了避免商品冲突  需要靠rec_id 去辨别
					state.selectedRecId.push(k.rec_id);
					return k.goods_id
				})
			})
			state.selectedList = selectedListArr.flat();
		},
		// 取消全选
		unSelectAll(state) {
			state.lists.forEach(v => {
				v.list.forEach(k => {
					// 设置选中状态
					k.checked = false;
					state.selectedRecId.push(k.rec_id);
				})
			})
			state.selectedList = [];
			state.selectedRecId = [];
		},
		// 库存的增加/减少
		numberChange(state, arr) {
			let arrIndex = arr[2].split(',')
			state.lists[arrIndex[0]].list[arrIndex[1]].goods_number = arr[0];
		},
		// 删除选中
		delGoods(state) {
			state.list = state.list.filter(v => {
				return state.selectedList.indexOf(v.goods_id) === -1
			})
			// 清空选中列表
			state.selectedList = [];
		},
		// 根据selectedList 里面的id 获取lists 指定商品
		filterList(state) {
			var arr = [];
			state.lists.forEach(v => {
				v.list.forEach(k => {
					if (state.selectedList.indexOf(k.goods_id) > -1) {
						arr.push(k);
					}
				})
			})
			consoe.log('arr',arr)
			state.selectedListObj = arr;
		},
		
		// 添加到商品订单
		setShopOrderArrInfo(state,goods){
			console.log('goods',goods)
			state.shopOrderArrInfo = goods;
		}
		
	},
	actions: {
		// 更新购物车列表
		updateCartList({
			state,
			commit
		}) {
			return new Promise((res, rej) => {
				$HTTP.post('/api/api.php', {
					act: 'get_cart'
				}, {
					token: true
				}).then(data => {
					// 取消选中状态
					if (data.error) {
						state.lists = [];
						return res(data)
					};
					commit('unSelectAll')
					// 赋值
					commit('initCartList', [data.lists])
					res(data)
				}).catch(err => {
					rej(err)
				})
			})
		},
		// 全选与反选
		doSelectAll({
			commit,
			getters
		}) {
			getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
		},
		// 多个删除
		doDelGoods({
			commit,
			dispatch,
			state
		}) {
			if (state.selectedList.length === 0) {
				return uni.showToast({
					title: '请选择要删除的商品',
					icon: 'none'
				});
			}
			uni.showModal({
				content: '是否删除选中',
				success: (res) => {
					if (res.confirm) {
						commit('filterList');
						let options = {
							act: 'edit_cart',
							type: 'del',
							goods: state.selectedListObj
						}
						$HTTP.postStatus('/dscshop/api/api.php', options, {
							token: true
						}).then(res => {
							
							dispatch('updateCartList', { }, { root: true })
								uni.showToast({
									title: data.msg,
									icon: 'none'
								});
						})
					}
				}
			});
		}

	}

}
